import requests
import  json

def fanyi(keyword):
    #定义url
    url ='http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule'
    #定义请求参数
    data={'i': keyword,'from':'AUTO','to': 'AUTO','smartresult': 'dict','client': 'fanyideskweb','salt': '1542276300318','sign': 'e3f4a0e549d1617f4a99da771757f4a7','doctype': 'json','version': '2.1','keyfrom': 'fanyi.web','action': 'FY_BY_REALTIME','typoResult': 'false'}
    #发送请求，爬取信息
    res = requests.post(url,data=data)
    #解析结果
    str_json=res.content.decode("utf-8")
    # print(str_json)
    #print(str_json)
    myjson=json.loads(str_json)
    print(myjson['translateResult'][0][0]['tgt'])##翻译结果

if __name__=='__main__':
    while True:
        keyword = input("请输入要翻译的词：")
        if keyword == 'q':
            break
        fanyi(keyword)